package com.ruoyi.system.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.StrUtil;
import com.ruoyi.common.constant.ErrorConstants;
import com.ruoyi.common.exception.base.BaseException;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.system.dao.PmsFirstAidDao;
import com.ruoyi.system.domain.dto.PmsFirstAidDto;
import com.ruoyi.system.domain.po.*;
import com.ruoyi.system.domain.vo.PmsFirstAidVo;
import com.ruoyi.system.mapper.PmsFirstAidMapper;
import com.ruoyi.system.mapper.PmsFirstAidRecordMapper;
import com.ruoyi.system.service.PmsFirstAidService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;


/**
 * @author nanyang
 */
@Service
public class PmsFirstAidServiceImpl implements PmsFirstAidService {
    
    @Resource
    private PmsFirstAidMapper pmsFirstAidMapper;

    @Resource
    private PmsFirstAidDao pmsFirstAidDao;

    @Resource
    private PmsFirstAidRecordMapper pmsFirstAidRecordMapper;
    
    
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Integer insertPmsFirstAid(PmsFirstAidDto.Insert pmsFirstAidInsert) {
        // 检查身份证号是否重复
        PmsFirstAidExample pmsFirstAidExample = new PmsFirstAidExample();
        pmsFirstAidExample.createCriteria().andDelFlagEqualTo((byte) 0).andIdCardEqualTo(pmsFirstAidInsert.getIdCard());
        List<PmsFirstAid> pmsFirstAids = pmsFirstAidMapper.selectByExample(pmsFirstAidExample);
        if (!pmsFirstAids.isEmpty()) {
            throw new BaseException(ErrorConstants.DUPLICATE_ID_CARD);
        }
        // 新增实体数据
        long firstAidId = IdUtil.getSnowflake().nextId();
        PmsFirstAid pmsFirstAid = BeanUtil.copyProperties(pmsFirstAidInsert, PmsFirstAid.class);
        pmsFirstAid.setId(firstAidId);
        pmsFirstAid.setCreateBy(SecurityUtils.getUsername());
        pmsFirstAid.setCreateTime(new Date());
        int i = pmsFirstAidMapper.insertSelective(pmsFirstAid);
        // 新增子表数据
        if (i > 0 && !pmsFirstAidInsert.getRecordDtoList().isEmpty()) {
            pmsFirstAidInsert.getRecordDtoList().forEach(recordDto -> {
                PmsFirstAidRecord pmsFirstAidRecord = BeanUtil.copyProperties(pmsFirstAidInsert.getRecordDtoList(), PmsFirstAidRecord.class);
                pmsFirstAidRecord.setId(IdUtil.getSnowflake().nextId());
                pmsFirstAidRecord.setFirstAidId(firstAidId);
                pmsFirstAidRecord.setCreatedAt(new Date());
                pmsFirstAidRecordMapper.insertSelective(pmsFirstAidRecord);
            });
        }
        return i;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Integer updatePmsFirstAid(PmsFirstAidDto.Update pmsFirstAidUpdate) {
        // 检查身份证号是否重复
        if (StrUtil.isNotBlank(pmsFirstAidUpdate.getIdCard())) {
            PmsFirstAidExample pmsFirstAidExample = new PmsFirstAidExample();
            pmsFirstAidExample.createCriteria().andDelFlagEqualTo((byte) 0).andIdCardEqualTo(pmsFirstAidUpdate.getIdCard());
            List<PmsFirstAid> pmsFirstAids = pmsFirstAidMapper.selectByExample(pmsFirstAidExample);
            if (!pmsFirstAids.isEmpty()) {
                throw new BaseException(ErrorConstants.DUPLICATE_ID_CARD);
            }
        }
        // 修改实体
        PmsFirstAid pmsFirstAid = BeanUtil.copyProperties(pmsFirstAidUpdate, PmsFirstAid.class);
        pmsFirstAid.setUpdateBy(SecurityUtils.getUsername());
        pmsFirstAid.setUpdateTime(new Date());
        int i = pmsFirstAidMapper.updateByPrimaryKeySelective(pmsFirstAid);
        if (i > 0 && !pmsFirstAidUpdate.getRecordDtoList().isEmpty()) {
            pmsFirstAidUpdate.getRecordDtoList().forEach(recordDto -> {
                PmsFirstAidRecord pmsFirstAidRecord = BeanUtil.copyProperties(pmsFirstAidUpdate.getRecordDtoList(), PmsFirstAidRecord.class);
                pmsFirstAidRecord.setAnswer(pmsFirstAidRecord.getAnswer());
                pmsFirstAidRecord.setUpdatedAt(new Date());
                PmsFirstAidRecordExample pmsFirstAidRecordExample = new PmsFirstAidRecordExample();
                pmsFirstAidRecordExample.createCriteria().andCodeEqualTo(recordDto.getCode()).andFirstAidIdEqualTo(pmsFirstAidUpdate.getId());
                pmsFirstAidRecordMapper.updateByExampleSelective(pmsFirstAidRecord, pmsFirstAidRecordExample);
            });
        }
        return i;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Integer deletePmsFirstAid(PmsFirstAidDto.Delete pmsFirstAidDelete) {
        PmsFirstAidExample firstAidExampleExample = new PmsFirstAidExample();
        firstAidExampleExample.createCriteria().andIdIn(pmsFirstAidDelete.getIdList());
        PmsFirstAid pmsFirstAid = new PmsFirstAid();
        pmsFirstAid.setDelFlag((byte) 2);
        int i = pmsFirstAidMapper.updateByExampleSelective(pmsFirstAid, firstAidExampleExample);
        if (i > 0) {
            PmsFirstAidRecordExample pmsFirstAidRecordExample = new PmsFirstAidRecordExample();
            pmsFirstAidRecordExample.createCriteria().andFirstAidIdIn(pmsFirstAidDelete.getIdList());
            PmsFirstAidRecord pmsFirstAidRecord = new PmsFirstAidRecord();
            pmsFirstAidRecord.setDelFlag((byte) 2);
            pmsFirstAidRecordMapper.updateByExampleSelective(pmsFirstAidRecord, pmsFirstAidRecordExample);
        }
        return i;
    }

    @Override
    public List<PmsFirstAidVo.Result> listPmsFirstAid(PmsFirstAidDto.Query queryPmsFirstAid) {
        return pmsFirstAidDao.select(queryPmsFirstAid);
    }

}
